import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.IO;

/**
 * Remark：将评语转化为等级
 */
public class Remark {
    /**
     * @评分等级制度
     */
    private static int cal;
    private static String rank = "null";
    static Map<Integer, String> rankCal;

    public static void remarkCalculator(String hwDir, String nameListFile, String hwName) {
        // 将名单读取到字符串列表
        List<String> lineList = IO.fileToStringList(nameListFile, StandardCharsets.UTF_8);
        // 返回个人名义下的作业目录
        List<String> dirList = Init.getDirList(lineList);
        // 解析作业名单
        List<String> hwList = Doraemon.compileHwList(hwName);
        // 定义评语录入映射
        Map<String, String> loadContent = new HashMap<>();
        for (String dir : dirList) {
            for (String hwLine : hwList) {
                // base加法器初始值
                int cal1 = 0;
                // positive加法器初始值
                int cal2 = 0;
                // negative加法器初值
                int cal3 = 0;
                // 获取个人每次作业文件
                String commentsFile = hwDir + File.separator + dir + File.separator + hwLine + ".txt";
                // 将每个学生的作业评价读取到字符串列表
                List<String> commentList = IO.fileToStringList(commentsFile, StandardCharsets.UTF_8);
                // 将每个学生获得的作业评价传入成绩加法器中进行遍历
                for (String commentLine : commentList) {
                    // base需要实现6个功能每实现一个功能，在一号累加器进行一次加分计算
                    if (commentLine.contains("*")) {
                        cal1 += 1;
                    }
                    // 5个加分点，每实现一个加分点在二号累加器里加1分
                    if (commentLine.contains("+")) {
                        cal2 += 1;
                    }
                    // 4个扣分点，每踩一次雷在三号累加器里扣1分
                    if (commentLine.contains("-")) {
                        cal3 -= 1;
                    }
                }
                    /**
                     * 就作业基础评分进行等级转换 1. cal1:(0,6) <R> 2. cal1 == 0 <L> 3. cal == 6
                     * <获得基础评分，转入加分减分项评价>
                     */
                    if (cal1 == 0) {
                        rank = "L";
                    } else if ((cal1 > 0) && (cal1 < 6)) {
                        rank = "R";
                    } else if (cal1 == 6) {
                        Remark.rankConfigMap();
                        cal = cal2 + cal3;
                        rank = rankCal.get(cal);
                    }
                    loadContent.put(hwLine, rank);
                    String rankStroe = hwLine + "," + rank;
                    String file = hwDir + File.separator + dir + File.separator + "rank.csv";
                    // 以下操作是将待处理资源暂时存在一个缓冲区中，以提高读取速度，增加性能
                    try {
                        FileWriter fileWriter = new FileWriter(file, true);
                        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                        bufferedWriter.write(rankStroe + "\n");
                        bufferedWriter.close();

                    } catch (IOException e) {
                        System.err.println(e);
                    }
                }
            }
        }
    
    /**
     * @成绩-等级转换映射
     */
    public static void rankConfigMap() {
        rankCal = new HashMap<>();
        rankCal.put(5, "S");
        rankCal.put(4, "S");
        rankCal.put(3, "A+");
        rankCal.put(2, "A+");
        rankCal.put(1, "A");
        rankCal.put(0, "A-");
        rankCal.put(-1, "B+");
        rankCal.put(-2, "B");
        rankCal.put(-3, "B-");
        rankCal.put(-4, "C");
    }

    public static void main(String[] args) {
        // javac -cp src/ -d dist/ scr/Remark.java
        // java -cp dist/ Doraemon config.txt
        // String nameListFile = "name-list.csv";
        // String hwDir = "hw";
        String hwName = "hw-10";
        String hwDir = "hw";
        String nameListFile = "name-list.csv";
        Remark.remarkCalculator(hwDir, nameListFile, hwName);

    }
}
